package com.bjpowernode.mvc.service.Impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.common.consts.AppConst;
import com.bjpowernode.common.consts.Keys;
import com.bjpowernode.db.domain.ProductInfo;
import com.bjpowernode.mvc.model.dto.product.ProductCollDTO;
import com.bjpowernode.mvc.service.BaseService;
import com.bjpowernode.mvc.service.inter.ProductService;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;


@Service
public class ProductServiceImpl extends BaseService implements ProductService {

    @Resource
    private RedisClient redisClient;
    @Override
    public ProductCollDTO showProducts() {

        ProductCollDTO dto=null;

        String strJSON=redisClient.getStr(Keys.LOGIN_LIST);

        if (StrUtil.isNotBlank(strJSON)){
            dto= JSONUtil.toBean(strJSON,ProductCollDTO.class);
        }else {
            synchronized (this){
                strJSON=redisClient.getStr(Keys.LOGIN_LIST);
                if (StrUtil.isBlank(strJSON)){
                    dto=getProductForDB();

                    redisClient.addStr(Keys.LOGIN_LIST,JSONUtil.toJsonStr(dto),120);
                }else {
                    dto=JSONUtil.toBean(strJSON,ProductCollDTO.class);
                }
            }
        }
        return dto;

    }

    private ProductCollDTO getProductForDB() {
        //查询新手宝产品;
        List<ProductInfo>xinList= queryByTypeInner(AppConst.TYPE_XIN_SHOU_BAO, 1);
        //查询优选l;
        List<ProductInfo>youList= queryByTypeInner(AppConst.TYPE_YOU_SHOU_XUAN, 3);
        //查询新手宝产品l;
        List<ProductInfo> sanList = queryByTypeInner(AppConst.TYPE_XIN_SAN_BTAO,3 );


        ProductCollDTO productCollDTO=new ProductCollDTO();
        productCollDTO.setXinList(xinList);
        productCollDTO.setYouList(youList);
        productCollDTO.setSanList(sanList);

        return productCollDTO;
    }


    //按照产品类型分页查询,使用mp插件
    @Override
    public Page<ProductInfo> queryPageByType(Integer type, Integer pageNo, Integer pageSize) {

        //QueryWrapper是mp提供的一个查询条件构造器,用于构造SQL查询条件
        QueryWrapper<ProductInfo> wrapper=new QueryWrapper<>();

        //排序.
        wrapper.eq("product_type", type)
                .orderByDesc("release_time","rate","id");

        Page<ProductInfo> page =new Page<>(pageNo,pageSize);

        return productInfoMapper.selectPage(page ,wrapper);
    }


    //产品详情.
    @Override
    public ProductInfo queryById(Integer id) {
        return productInfoMapper.selectById(id);
    }



    private List<ProductInfo> queryByTypeInner(Integer type,Integer rows){
        QueryWrapper<ProductInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_type", type).orderByDesc("release_time","rate","id").last("limit 0," +rows);

        return productInfoMapper.selectList(queryWrapper);
    }
}
